{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import confusion_matrix\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Pregnancies_tfidf</th>\n",
       "      <th>Glucose_tfidf</th>\n",
       "      <th>BloodPressure_tfidf</th>\n",
       "      <th>SkinThickness_tfidf</th>\n",
       "      <th>Insulin_tfidf</th>\n",
       "      <th>BMI_tfidf</th>\n",
       "      <th>DiabetesPedigreeFunction_tfidf</th>\n",
       "      <th>Age_tfidf</th>\n",
       "      <th>Outcome</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>0.326047</td>\n",
       "      <td>0.724331</td>\n",
       "      <td>0.408045</td>\n",
       "      <td>0.390245</td>\n",
       "      <td>0.561096</td>\n",
       "      <td>0.372008</td>\n",
       "      <td>0.239969</td>\n",
       "      <td>0.494433</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.057579</td>\n",
       "      <td>0.443948</td>\n",
       "      <td>0.490637</td>\n",
       "      <td>0.411807</td>\n",
       "      <td>0.729136</td>\n",
       "      <td>0.372960</td>\n",
       "      <td>0.163360</td>\n",
       "      <td>0.370423</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.405261</td>\n",
       "      <td>0.871425</td>\n",
       "      <td>0.316210</td>\n",
       "      <td>0.286673</td>\n",
       "      <td>0.513412</td>\n",
       "      <td>0.199944</td>\n",
       "      <td>0.237597</td>\n",
       "      <td>0.259274</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.067613</td>\n",
       "      <td>0.588317</td>\n",
       "      <td>0.574267</td>\n",
       "      <td>0.367221</td>\n",
       "      <td>0.614765</td>\n",
       "      <td>0.471017</td>\n",
       "      <td>0.078123</td>\n",
       "      <td>0.267423</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>0.201212</td>\n",
       "      <td>0.600166</td>\n",
       "      <td>0.164476</td>\n",
       "      <td>0.367945</td>\n",
       "      <td>0.735875</td>\n",
       "      <td>0.476612</td>\n",
       "      <td>0.887961</td>\n",
       "      <td>0.279442</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Pregnancies_tfidf  Glucose_tfidf  BloodPressure_tfidf  SkinThickness_tfidf  \\\n",
       "0           0.326047       0.724331             0.408045             0.390245   \n",
       "1           0.057579       0.443948             0.490637             0.411807   \n",
       "2           0.405261       0.871425             0.316210             0.286673   \n",
       "3           0.067613       0.588317             0.574267             0.367221   \n",
       "4           0.201212       0.600166             0.164476             0.367945   \n",
       "\n",
       "   Insulin_tfidf  BMI_tfidf  DiabetesPedigreeFunction_tfidf  Age_tfidf  \\\n",
       "0       0.561096   0.372008                        0.239969   0.494433   \n",
       "1       0.729136   0.372960                        0.163360   0.370423   \n",
       "2       0.513412   0.199944                        0.237597   0.259274   \n",
       "3       0.614765   0.471017                        0.078123   0.267423   \n",
       "4       0.735875   0.476612                        0.887961   0.279442   \n",
       "\n",
       "   Outcome  \n",
       "0        1  \n",
       "1        0  \n",
       "2        1  \n",
       "3        0  \n",
       "4        1  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('diabetes_tfidf.csv')\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train = train['Outcome']\n",
    "x_train = train.drop(['Outcome'],axis=1)\n",
    "feature_names = x_train.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(614, 8)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train_part,x_val,y_train_part,y_val = train_test_split(x_train,y_train,train_size = 0.8,test_size = 0.2)\n",
    "print(x_train_part.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "from sklearn.model_selection import GridSearchCV "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=5, error_score='raise-deprecating',\n",
       "             estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "                           decision_function_shape='ovr', degree=3,\n",
       "                           gamma='auto_deprecated', kernel='rbf', max_iter=-1,\n",
       "                           probability=False, random_state=None, shrinking=True,\n",
       "                           tol=0.001, verbose=False),\n",
       "             iid='warn', n_jobs=None,\n",
       "             param_grid={'C': array([1.e-01, 1.e+00, 1.e+01, 1.e+02, 1.e+03]),\n",
       "                         'gamma': array([ 0.1,  1. , 10. ])},\n",
       "             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,\n",
       "             scoring='accuracy', verbose=0)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C_s = np.logspace(-1,3,5)\n",
    "gamma_s = np.logspace(-1,1,3)\n",
    "tuned_parameters = dict(gamma = gamma_s, C = C_s)\n",
    "svc_rbf = SVC(kernel='rbf')\n",
    "grid = GridSearchCV(svc_rbf,tuned_parameters,cv = 5,scoring = 'accuracy')\n",
    "grid.fit(x_train_part,y_train_part)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.6921824104234527\n",
      "{'C': 100.0, 'gamma': 1.0}\n"
     ]
    }
   ],
   "source": [
    "# 打印分数 和参数\n",
    "print(grid.best_score_)\n",
    "print(grid.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=100, cache_size=200, class_weight=None, coef0=0.0,\n",
       "    decision_function_shape='ovr', degree=3, gamma=1, kernel='rbf', max_iter=-1,\n",
       "    probability=False, random_state=None, shrinking=True, tol=0.001,\n",
       "    verbose=False)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Best_C = 100\n",
    "Best_gamma = 1\n",
    "svc = SVC(C = Best_C,gamma=Best_gamma,kernel='rbf',probability=False)\n",
    "svc.fit(x_train_part,y_train_part)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test score: 0.7337662337662337 \n"
     ]
    }
   ],
   "source": [
    "print('test score: {} '.format(svc.score(x_val, y_val)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
